$(function () {
    $('#save-cuser-btn').click(function () {
        var usernameE = $('input[name="username"]');
        var emailE = $('input[name="email"]');
        var pswE = $('input[name="password"]');
        var roleSelect = $('select[name="role_id"]');

        var username = usernameE.val();
        var email = emailE.val();
        var password = pswE.val();
        var role_id = roleSelect.val();

        zlajax.post({
            'url': '/cms/acuser/',
            'data': {
                'username': username,
                'email': email,
                'password': password,
                'role_id': role_id
            },
            'success': function (data) {
                if (data['code'] == 200) {
                    //隐藏模态框
                    $('#myModal').modal('hide');
                    xtalert.alertSuccessToast('添加成功！');
                    setTimeout(function () {
                        window.location.reload();
                    }, 1000)
                } else {
                    xtalert.alertInfo(data['message']);
                }
            },
            'fail': function (error) {
                xtalert.alertNetworkError('网络错误！')
            }
        })
    });
});

$(function () {
    $('.edit-auth-btn').click(function () {
        var self = $(this);
        var tr = self.parent().parent();
        var cuser_id = tr.attr('data-user-id');
        var role_name = tr.attr('data-role-name');

        xtalert.alertOneInput({
            'title': '填写权限名称',
            'text': '当前的权限是【' + role_name+'】',
            'confirmCallback': function (inputValue) {
                zlajax.post({
                    'url': '/cms/updateAuth/',
                    'data': {
                        'cuser_id' : cuser_id,
                        'name': inputValue
                    },
                    'success': function (data) {
                        if (data['code'] == 200) {
                            xtalert.alertSuccessToast('修改成功！');
                            setTimeout(function () {
                                window.location.reload();
                            }, 1000)
                        } else {
                            xtalert.alertInfo(data['message']);
                        }
                    },
                    'fail': function (error) {
                        xtalert.alertNetworkError('网络错误！')
                    }
                })
            }
        });
    });
});


$(function () {
    $('.move-blackList').click(function (event) {
        var self = $(this);
        var tr = self.parent().parent();
        var cuser_id = tr.attr('data-user-id');
        var status = parseInt(tr.attr('data-status'));
        var url = "";
        if (status == 1) {
            url = '/cms/inblacklist/';
        } else {
            url = '/cms/outblacklist/';
        }
        zlajax.post({
            'url': url,
            'data': {
                'cuser_id': cuser_id
            },
            'success': function (data) {
                if (data['code'] == 200) {
                    xtalert.alertSuccessToast('操作成功！');
                    setTimeout(function () {
                        window.location.reload();
                    }, 1000)
                } else {
                    xtalert.alertInfo(data['message']);
                }
            },
            'fail': function (error) {
                xtalert.alertNetworkError('网络错误！');
            }
        })
    });
});